<!DOCTYPE html><html class="theme-next pisces use-motion" lang="zh-CN"><head><meta name="generator" content="Hexo 3.9.0"><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><link rel="apple-touch-icon" sizes="180x180" href="/images/icons/favicon-48.png?v=6.7.0"><link rel="icon" type="image/png" sizes="32x32" href="/images/icons/favicon-32.png?v=6.7.0"><link rel="icon" type="image/png" sizes="16x16" href="/images/icons/favicon-16.png?v=6.7.0"><link rel="mask-icon" href="/images/icons/logo.svg?v=6.7.0" color="#222"><link rel="alternate" href="/atom.xml" title="菠菜眾長" type="application/atom+xml"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/fancybox/source/jquery.fancybox.css" rel="stylesheet" type="text/css"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=6.7.0" rel="stylesheet" type="text/css"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Pisces",version:"6.7.0",sidebar:{position:"left",display:"always",offset:12,b2t:!1,scrollpercent:!0,onmobile:!0},fancybox:!0,fastclick:!0,lazyload:!0,tabs:!0,motion:{enable:!0,async:!0,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}},algolia:{applicationID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}}}</script><meta name="baidu-site-verification" content="B2fOjPkYa7"><meta name="google-site-verification" content="OhdtVOx5uwpZ_mMm0AZJXzw-dY1PPpAAkdavmmQhIL4"><meta name="360-site-verification" content="5b1c9d7574859ca6e460dd687667d5dc"><meta name="shenma-site-verification" content="933461d02e7b7c40e5293ee90085127c_1569650330"><meta name="description" content="面向对象思想(理解)面向对象是基于面向过程的一种编程思想思想特点：A:是一种更符合我们思考习惯的思想B:把复杂的问题简单化C:让我们从执行者变成了指挥者举例：A:洗衣服B:吃饭C:买电脑举例并代码体现把大象装进冰箱类与对象(掌握)我们学习编程是为了把现实世界的事物用编程语言描述来实现信息化。现实世界事物是如何表达的呢?属性：外在特征行为：内在行为我们学习的是java语言，它最基本的单位是类。所以我"><meta name="keywords" content="JAVA,面向对象"><meta property="og:type" content="article"><meta property="og:title" content="面向对象基础知识总结"><meta property="og:url" content="https://lruihao.cn/posts/duixiang.html"><meta property="og:site_name" content="菠菜眾長"><meta property="og:description" content="面向对象思想(理解)面向对象是基于面向过程的一种编程思想思想特点：A:是一种更符合我们思考习惯的思想B:把复杂的问题简单化C:让我们从执行者变成了指挥者举例：A:洗衣服B:吃饭C:买电脑举例并代码体现把大象装进冰箱类与对象(掌握)我们学习编程是为了把现实世界的事物用编程语言描述来实现信息化。现实世界事物是如何表达的呢?属性：外在特征行为：内在行为我们学习的是java语言，它最基本的单位是类。所以我"><meta property="og:locale" content="zh-CN"><meta property="og:updated_time" content="2019-10-19T02:54:56.864Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="面向对象基础知识总结"><meta name="twitter:description" content="面向对象思想(理解)面向对象是基于面向过程的一种编程思想思想特点：A:是一种更符合我们思考习惯的思想B:把复杂的问题简单化C:让我们从执行者变成了指挥者举例：A:洗衣服B:吃饭C:买电脑举例并代码体现把大象装进冰箱类与对象(掌握)我们学习编程是为了把现实世界的事物用编程语言描述来实现信息化。现实世界事物是如何表达的呢?属性：外在特征行为：内在行为我们学习的是java语言，它最基本的单位是类。所以我"><link rel="alternate" href="/atom.xml" title="菠菜眾長" type="application/atom+xml"><link rel="canonical" href="https://lruihao.cn/posts/duixiang.html"><script type="text/javascript" id="page.configurations">CONFIG.page={sidebar:""}</script><title>面向对象基础知识总结 | 菠菜眾長</title><script type="text/javascript">var _hmt=_hmt||[];!function(){var e=document.createElement("script");e.src="https://hm.baidu.com/hm.js?d25f1e053205bf07562f33365fef04d7";var t=document.getElementsByTagName("script")[0];t.parentNode.insertBefore(e,t)}()</script><noscript><style type="text/css">.sidebar-inner,.use-motion .brand,.use-motion .collection-title,.use-motion .comments,.use-motion .menu-item,.use-motion .motion-element,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .logo,.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.logo-line-after i{right:initial}</style></noscript></head><body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN"><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">菠菜众长</span> <span class="logo-line-after"><i></i></span></a></div><h1 class="site-subtitle" itemprop="description">李瑞豪的博客</h1></div><div class="site-nav-toggle"><button aria-label="切换导航栏"><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i><br>首页</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i><br>归档<span class="badge">176</span></a></li><li class="menu-item menu-item-docs"><a href="/docs/" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i><br>综合</a></li><li class="menu-item menu-item-album"><a href="https://img.lruihao.cn" rel="external nofollow noopener noreferrer" target="_blank"><i class="menu-item-icon fa fa-fw fa-image"></i><br>相册</a></li><li class="menu-item menu-item-guestbook"><a href="/guestbook/" rel="section"><i class="menu-item-icon fa fa-fw fa-comments"></i><br>留言</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-address-card"></i><br>关于</a></li><li class="menu-item menu-item-search"><a href="javascript:;" class="popup-trigger"><i class="menu-item-icon fa fa-search fa-fw"></i><br>搜索</a></li></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"><div style="text-align:center;padding:3px 0 0"><div style="margin-top:20px;font-size:18px;font-weight:600;border-bottom:1px solid #ccc"><i class="fa fa-history" aria-hidden="true"></i> 近期文章</div><ul style="margin:0;padding:0;list-style:none"><li><a href="/posts/csdnvisiter.html" title="使用Python刷csdn访问量" target="_blank">使用Python刷csdn访问量</a></li><li><a href="/posts/cron.html" title="CRON表达式的基本语法" target="_blank">CRON表达式的基本语法</a></li><li><a href="/posts/daka.html" title="新冠疫情未返校未返工第N天之“自动打卡”" target="_blank">新冠疫情未返校未返工第N天之“自动打卡”</a></li><li><a href="/posts/year-2019.html" title="2019年度总结" target="_blank">2019年度总结</a></li><li><a href="/posts/sql.html" title="SQL总结" target="_blank">SQL总结</a></li><li><a href="/posts/cos-album.html" title="利用腾讯云为静态页面添加“动态”相册" target="_blank">利用腾讯云为静态页面添加“动态”相册</a></li><li><a href="/posts/restful.html" title="RESTful" target="_blank">RESTful</a></li><li><a href="/posts/phpPushUrl.html" title="php同时主动推送链接到百度，神马等站长平台" target="_blank">php同时主动推送链接到百度，神马等站长平台</a></li><li><a href="/posts/phpfile.html" title="php按行读取文件信息" target="_blank">php按行读取文件信息</a></li><li><a href="/posts/site-time.html" title="设置网站运行时间" target="_blank">设置网站运行时间</a></li><li><a href="/posts/async-defer.html" title="script的三种加载方式（async、defer）" target="_blank">script的三种加载方式（async、defer）</a></li><li><a href="/posts/Sublime-Text3.html" title="Sublime Text3快捷键大全" target="_blank">Sublime Text3快捷键大全</a></li><li><a href="/posts/netBeans.html" title="netBeans IDE开发设置" target="_blank">netBeans IDE开发设置</a></li><li><a href="/posts/dev-rules.html" title="web开发规则，代码规范" target="_blank">web开发规则，代码规范</a></li><li><a href="/posts/phpform.html" title="简单评论模块--php表单练习" target="_blank">简单评论模块--php表单练习</a></li></ul></div></div></div></div></nav></div></header><a href="https://github.com/Lruihao/lruihao.github.io" class="github-corner" target="_blank" title="万水千山总是情,给个star行不行！" aria-label="万水千山总是情,给个star行不行！" rel="external nofollow noopener noreferrer"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#222;color:#fff;position:absolute;top:0;border:0;right:0" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><div class="reading-progress-bar"></div><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><div class="post-block"><link itemprop="mainEntityOfPage" href="https://lruihao.cn/posts/duixiang.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="李瑞豪"><meta itemprop="description" content="从ACM到Web，分享程序、技巧、干货，记录心情、学习、成长！"><meta itemprop="image" content="/images/avatar.png"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="菠菜眾長"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">面向对象基础知识总结<a href="https://github.com/Lruihao/lruihao.github.io/tree/hexo/source/_posts/duixiang.md" class="post-edit-link" title="编辑" rel="external nofollow noopener noreferrer" target="_blank"><i class="fa fa-pencil"></i></a></h2><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2019-01-15 16:07:31" itemprop="dateCreated datePublished" datetime="2019-01-15T16:07:31+08:00">2019-01-15</time> </span><span class="post-category"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-folder-o"></i> </span><span class="post-meta-item-text">分类于</span> <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Backend/" itemprop="url" rel="index"><span itemprop="name">Backend</span></a></span> ， <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Backend/JAVA/" itemprop="url" rel="index"><span itemprop="name">JAVA</span></a></span> </span><span class="post-comments-count"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><span class="post-meta-item-text">评论数： </span><a href="/posts/duixiang.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/duixiang.html" itemprop="commentCount"></span> </a></span><span id="/posts/duixiang.html" class="leancloud_visitors" data-flag-title="面向对象基础知识总结"><span class="post-meta-divider">|</span> <span title="阅读次数"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">阅读次数：</span> <span class="leancloud-visitors-count"></span></span></span><div class="post-symbolscount"><span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">本文字数：</span> <span title="本文字数">3,312</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span title="阅读时长">8 分钟</span></div></div></header><div class="post-body" itemprop="articleBody"><h3 id="面向对象思想-理解"><a href="#面向对象思想-理解" class="headerlink" title="面向对象思想(理解)"></a>面向对象思想(理解)</h3><ol><li>面向对象是基于面向过程的一种编程思想</li><li>思想特点：<br>A:是一种更符合我们思考习惯的思想<br>B:把复杂的问题简单化<br>C:让我们从执行者变成了指挥者</li><li>举例：<br>A:洗衣服<br>B:吃饭<br>C:买电脑</li><li>举例并代码体现<br>把大象装进冰箱</li></ol><h3 id="类与对象-掌握"><a href="#类与对象-掌握" class="headerlink" title="类与对象(掌握)"></a>类与对象(掌握)</h3><ol><li>我们学习编程是为了把现实世界的事物用编程语言描述来实现信息化。</li><li>现实世界事物是如何表达的呢?<br>属性：外在特征<br>行为：内在行为</li><li>我们学习的是java语言，它最基本的单位是类。<br>所以我们要学会用类来体现一个事物。</li><li>类：是一组相关的属性和行为的集合</li><li>对象：是该类事物的具体个体。</li><li>举例：<br>学生 类<br>张三 对象</li></ol><h3 id="类的组成-掌握"><a href="#类的组成-掌握" class="headerlink" title="类的组成(掌握)"></a>类的组成(掌握)</h3><ol><li>成员变量<br>其实就是变量，只不过定义在类中，方法外，并且可以不用初始化。</li><li>成员方法<br>其实就是方法，只不过不需要static了</li><li>案例：<br>学生类<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	String name;</span><br><span class="line">	<span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">study</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li></ol><h3 id="类的使用-掌握"><a href="#类的使用-掌握" class="headerlink" title="类的使用(掌握)"></a>类的使用(掌握)</h3><ol><li>创建对象<br>格式：类名 对象名 = new 类名();</li><li>使用成员<br>成员变量：对象名.变量名;<br>成员方法：对象名.方法名(…);</li></ol><h3 id="成员变量和局部变量的区别-理解"><a href="#成员变量和局部变量的区别-理解" class="headerlink" title="成员变量和局部变量的区别(理解)"></a>成员变量和局部变量的区别(理解)</h3><ol><li>在类中的位置不同<br>A:成员变量 类中，方法外<br>B:局部变量 方法的形式参数，或者方法体中</li><li>在内存中的位置不同<br>A:成员变量 在堆中<br>B:局部变量 在栈中</li><li>生命周期不同<br>A:成员变量 随着对象的存在而存在，随着对象的消失而消失<br>B:局部变量 随着方法的调用而存在，随着方法的调用完毕而消失</li><li>初始化值不同<br>A:成员变量 有默认初始化值<br>B:局部变量 没有默认值，必须先声明，赋值，最后才能使用</li></ol><h3 id="形式参数问题-理解"><a href="#形式参数问题-理解" class="headerlink" title="形式参数问题(理解)"></a>形式参数问题(理解)</h3><ol><li>基本类型<br>基本类型作为形式参数，需要的是该基本类型的值。</li><li>引用类型<br>引用类型作为形式参数，需要的是该引用类型的地址值。(对象)</li></ol><h3 id="匿名对象-理解"><a href="#匿名对象-理解" class="headerlink" title="匿名对象(理解)"></a>匿名对象(理解)</h3><ol><li>匿名对象：没有名字的对象。是对象的简化书写方式。</li><li>使用场景<br>A:调用方法，仅仅只调用一次<br>B:作为实际参数传递</li></ol><h3 id="封装-掌握"><a href="#封装-掌握" class="headerlink" title="封装(掌握)"></a>封装(掌握)</h3><ol><li>隐藏实现细节，提供公共的访问方式</li><li>好处：<br>A:隐藏实现细节，提供公共的访问方式<br>B:提高了代码的复用性<br>C:提高了代码的安全性</li><li>使用原则<br>A:把成员变量隐藏<br>B:给出该成员变量对应的公共访问方式</li></ol><h3 id="private关键字-掌握"><a href="#private关键字-掌握" class="headerlink" title="private关键字(掌握)"></a>private关键字(掌握)</h3><ol><li>是一个权限修饰符</li><li>可以修饰类的成员(成员变量和成员方法)</li><li>仅仅在本类中可以访问,对外提供对应的GetXXX()，SetXXX()等方法</li><li>标准代码：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String n)</span> </span>&#123;</span><br><span class="line">		name = n;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> a)</span> </span>&#123;</span><br><span class="line">		age = a;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">study</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li></ol><h3 id="this关键字-掌握"><a href="#this关键字-掌握" class="headerlink" title="this关键字(掌握)"></a>this关键字(掌握)</h3><ol><li>this：代表本类的对象</li><li>应用场景：<br>解决了局部变量隐藏成员变量的问题。<br>其他用法和super一起讲。</li><li>标准代码：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;<span class="comment">//局部变量</span></span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"姓名是："</span>+name+<span class="string">",年龄是："</span>+age);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">study</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"学生爱学习"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"学生要吃饭"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sleep</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"学生想睡觉"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StudentTest</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		Student s = <span class="keyword">new</span> Student();</span><br><span class="line"></span><br><span class="line">		s.setName(<span class="string">"林青霞"</span>);</span><br><span class="line">		s.setAge(<span class="number">28</span>);</span><br><span class="line"></span><br><span class="line">		s.show();</span><br><span class="line">		s.study();</span><br><span class="line">		s.eat();</span><br><span class="line">		s.sleep();</span><br><span class="line"></span><br><span class="line">		System.out.println(<span class="string">"姓名是："</span>+s.getName());</span><br><span class="line">		System.out.println(<span class="string">"年龄是："</span>+s.getAge());</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li></ol><h3 id="构造方法-掌握"><a href="#构造方法-掌握" class="headerlink" title="构造方法(掌握)"></a>构造方法(掌握)</h3><ol><li>作用：对对象的数据进行初始化。</li><li>特点：<br>A:方法名和类名相同<br>B:没有返回值类型<br>C:没有返回值</li><li>注意事项<br>A:如果我们没写构造方法，系统将默认给出无参构造方法<br>B:如果我们写了构造方法，系统将不再给出默认无参构造方法<br>建议：我们自己手动给出无参构造方法</li><li>给成员变量赋值：<br>A:无参+setXxx()<br>B:带参</li><li>一个标准的代码：<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(String name,<span class="keyword">int</span> age)</span> </span>&#123;<span class="comment">//构造方法</span></span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"姓名是："</span>+name+<span class="string">",年龄是："</span>+age);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StudentTest</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//无参+setXxx()</span></span><br><span class="line">		Student s = <span class="keyword">new</span> Student();</span><br><span class="line">		s.setName(<span class="string">"林青霞"</span>);</span><br><span class="line">		s.setAge(<span class="number">28</span>);</span><br><span class="line">		s.show();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//带参</span></span><br><span class="line">		Student ss = <span class="keyword">new</span> Student(<span class="string">"张曼玉"</span>,<span class="number">20</span>);</span><br><span class="line">		ss.show();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li></ol><h3 id="对象的初始化过程-理解"><a href="#对象的初始化过程-理解" class="headerlink" title="对象的初始化过程(理解)"></a>对象的初始化过程(理解)</h3><ul><li>Student s = new Student();做了哪些事情<br>A:加载Student.class文件进内存<br>B:在栈中为s开辟空间<br>C:在堆中为学生对象开辟空间<br>D:为学生对象的成员变量赋默认值<br>E:为学生对象的成员变量赋显示值<br>F:通过构造方法给成员变量赋值<br>G:对象构造完毕，把地址赋值给s变量</li></ul><h3 id="static关键字-掌握"><a href="#static关键字-掌握" class="headerlink" title="static关键字(掌握)"></a>static关键字(掌握)</h3><ol><li>是一个状态修饰符。静态的意思</li><li>它可以修饰成员变量和成员方法</li><li>特点：<br><strong>A:随着类的加载而加载<br>B:优先于对象存在<br>C:被所有对象共享</strong><br>这也是判断我们是不是该使用静态的条件<br>举例：饮水机(可共享static)和水杯例子。<br>D:可以通过类名调用<br>静态修饰的内容，可以通过类名调用，也可以通过对象名调用</li><li>方法访问特点<br>A:普通成员方法<br>可以访问静态成员变量，非静态成员变量，静态成员方法，非静态成员方法<br>B:静态成员方法<br><strong>只能访问静态成员变量，静态成员方法<br>简记：静态只能访问静态</strong><br>注意：<br><strong>静态中是不能有this的。<br>先进内存的不能访问后进内存的。反之可以。</strong></li></ol><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	班级编号应该是被班级每个人都共享的，所以定义一个就应该可以了。</span></span><br><span class="line"><span class="comment">	而姓名和年龄，每个人应该是不一样的，所以，每个对象，都应该定义自己的。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">	在java中，用什么来表示成员变量是被共享的呢? static</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	<span class="comment">//姓名</span></span><br><span class="line">	String name;</span><br><span class="line">	<span class="comment">//年龄</span></span><br><span class="line">	<span class="keyword">int</span> age;</span><br><span class="line">	<span class="comment">//班级编号</span></span><br><span class="line">	<span class="comment">//String classNumber;</span></span><br><span class="line">	<span class="keyword">static</span> String classNumber;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(String name,<span class="keyword">int</span> age)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Student</span><span class="params">(String name,<span class="keyword">int</span> age,String classNumber)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.age = age;</span><br><span class="line">		<span class="keyword">this</span>.classNumber = classNumber;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(name+<span class="string">"---"</span>+age+<span class="string">"---"</span>+classNumber);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StudentDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//创建学生对象</span></span><br><span class="line">		Student s1 = <span class="keyword">new</span> Student(<span class="string">"林青霞"</span>,<span class="number">28</span>,<span class="string">"20150306"</span>);</span><br><span class="line">		s1.show();</span><br><span class="line"></span><br><span class="line">		<span class="comment">/*</span></span><br><span class="line"><span class="comment">		Student s2 = new Student("马云",35,"20150306");</span></span><br><span class="line"><span class="comment">		s2.show();</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">		Student s3 = new Student("马化腾",33,"20150306");</span></span><br><span class="line"><span class="comment">		s3.show();</span></span><br><span class="line"><span class="comment">		*/</span></span><br><span class="line">		Student s2 = <span class="keyword">new</span> Student(<span class="string">"马云"</span>,<span class="number">35</span>);</span><br><span class="line">		s2.show();</span><br><span class="line"></span><br><span class="line">		Student s3 = <span class="keyword">new</span> Student(<span class="string">"马化腾"</span>,<span class="number">33</span>);</span><br><span class="line">		s3.show();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	static:静态关键字。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">	作用：</span></span><br><span class="line"><span class="comment">		可以修饰成员变量和成员方法</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">	特点：</span></span><br><span class="line"><span class="comment">		A:随着类的加载而加载	</span></span><br><span class="line"><span class="comment">		B:优先于对象存在</span></span><br><span class="line"><span class="comment">		C:被类的所有对象共享</span></span><br><span class="line"><span class="comment">			这也是我们判断是否使用静态关键字的条件</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">			饮水机：可以被静态修饰</span></span><br><span class="line"><span class="comment">			水杯：不可以被静态修饰</span></span><br><span class="line"><span class="comment">		D:可以通过类名调用</span></span><br><span class="line"><span class="comment">			我们的调用既可以是对象，还可以是类名</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"show"</span>);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">show2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"show2"</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StudentDemo2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		Student s = <span class="keyword">new</span> Student();</span><br><span class="line">		s.show();</span><br><span class="line">		s.show2();</span><br><span class="line"></span><br><span class="line">		Student.show2();</span><br><span class="line">		<span class="comment">//Student.show();&amp;ensp;</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	static的注意事项：</span></span><br><span class="line"><span class="comment">		A:在静态方法中是没有this关键字的</span></span><br><span class="line"><span class="comment">			因为静态是随着类的加载而加载，优先于对象而存在。而this是随着对象的创建而存在。</span></span><br><span class="line"><span class="comment">			先进内存的， 不能访问后进内存的；而后进内存的，可以访问先进内存的。</span></span><br><span class="line"><span class="comment">		B:静态只能访问静态。</span></span><br><span class="line"><span class="comment">			非静态的成员方法：</span></span><br><span class="line"><span class="comment">				可以访问静态成员变量，非静态成员变量，静态成员方法，非静态成员方法</span></span><br><span class="line"><span class="comment">			静态的成员方法：</span></span><br><span class="line"><span class="comment">				只能访问静态的成员变量，静态的成员方法</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">class Student &#123;</span></span><br><span class="line"><span class="comment">	private String name;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">	public static void setName(String name) &#123;//&amp;ensp;静态方法不能用this</span></span><br><span class="line"><span class="comment">		this.name = name;</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">	public void show() &#123;</span></span><br><span class="line"><span class="comment">		System.out.println(name);</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">&#125;*/</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">	<span class="keyword">int</span> x = <span class="number">10</span>;</span><br><span class="line">	<span class="keyword">static</span> <span class="keyword">int</span> y = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(x);</span><br><span class="line">		System.out.println(y);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">show2</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//System.out.println(x);&amp;ensp;</span></span><br><span class="line">		System.out.println(y);<span class="comment">//√</span></span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show3</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		show();</span><br><span class="line">		show2();</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">show4</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">//show();只能访问静态的成员方法</span></span><br><span class="line">		show2();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">StudentDemo3</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//Student.setName("林青霞");</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="静态成员变量和普通成员变量的区别-理解"><a href="#静态成员变量和普通成员变量的区别-理解" class="headerlink" title="静态成员变量和普通成员变量的区别(理解)"></a>静态成员变量和普通成员变量的区别(理解)</h3><ol><li>所属不同<br>静态属于类的，称为类变量<br>非静态属于对象的，称为对象变量，实例变量</li><li>内存空间不同<br>静态在方法区的静态区<br>非静态在堆内存</li><li>生命周期不同<br>静态随着类的加载而加载，随着类的消失而消失<br>非静态随着对象的创建而存在，随着对象的消失而消失</li><li>调用不同<br>静态可以通过类名调用，也可以通过对象名调用。建议通过类名调用<br>非静态只能通过对象名调用</li></ol><h3 id="main方法是静态的-理解"><a href="#main方法是静态的-理解" class="headerlink" title="main方法是静态的(理解)"></a>main方法是静态的(理解)</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span></span><br></pre></td></tr></table></figure><p>public:访问权限修饰符，表示最大的访问权限，被jvm调用，所有权限要够大。<br>static:被jvm调用，不用创建对象，直接类名访问<br>void:被jvm调用，不需要给jvm返回值<br>main:一个通用的名称，虽然不是关键字，但是被jvm识别<br>String[] args:<br>早期出现是为了接收键盘录入数据的。</p><h3 id="实例"><a href="#实例" class="headerlink" title="实例"></a>实例</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*求和*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> x;</span><br><span class="line">	<span class="keyword">private</span> <span class="keyword">int</span> y;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Demo</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Demo</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.x = x;</span><br><span class="line">		<span class="keyword">this</span>.y = y;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setX</span><span class="params">(<span class="keyword">int</span> x)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.x = x;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getX</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> x;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setY</span><span class="params">(<span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.y = y;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getY</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> y;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="comment">//成员变量已经有x,y了。这里就没有必要在定义了</span></span><br><span class="line">	<span class="comment">/*</span></span><br><span class="line"><span class="comment">	public int sum(int x,int y) &#123;</span></span><br><span class="line"><span class="comment">		return x + y;</span></span><br><span class="line"><span class="comment">	&#125;</span></span><br><span class="line"><span class="comment">	*/</span></span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">sum</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> x + y;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		Demo d = <span class="keyword">new</span> Demo();</span><br><span class="line">		d.setX(<span class="number">10</span>);</span><br><span class="line">		d.setY(<span class="number">20</span>);</span><br><span class="line">		<span class="keyword">int</span> result = d.sum();</span><br><span class="line">		System.out.println(result);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*求和*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Demo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">sum</span><span class="params">(<span class="keyword">int</span> x,<span class="keyword">int</span> y)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> x + y;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test2</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		Demo d = <span class="keyword">new</span> Demo();</span><br><span class="line">		<span class="keyword">int</span> result = d.sum(<span class="number">10</span>,<span class="number">20</span>);</span><br><span class="line">		System.out.println(result);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">	定义一个员工类,自己分析出几个成员，</span></span><br><span class="line"><span class="comment">	然后给出成员变量，构造方法，getXxx()/setXxx()方法，</span></span><br><span class="line"><span class="comment">	以及一个显示所有成员信息的方法。并测试。</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">	Employee：</span></span><br><span class="line"><span class="comment">		成员变量：员工编号,姓名,职位</span></span><br><span class="line"><span class="comment">		构造方法：无参，带参</span></span><br><span class="line"><span class="comment">		成员方法：getXxx()/setXxx()方法，show()</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Employee</span> </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> String eid;</span><br><span class="line">	<span class="keyword">private</span> String name;</span><br><span class="line">	<span class="keyword">private</span> String job;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Employee</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Employee</span><span class="params">(String eid,String name,String job)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.eid = eid;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">		<span class="keyword">this</span>.job = job;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setEid</span><span class="params">(String eid)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.eid = eid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getEid</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> eid;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.name = name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> name;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setJob</span><span class="params">(String job)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.job = job;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> String <span class="title">getJob</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> job;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">show</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		System.out.println(<span class="string">"员工编号是："</span>+eid+<span class="string">",姓名是："</span>+name+<span class="string">",职位是："</span>+job);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">EmployeeDemo</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		<span class="comment">//无参</span></span><br><span class="line">		Employee e = <span class="keyword">new</span> Employee();</span><br><span class="line">		e.setEid(<span class="string">"itcast007"</span>);</span><br><span class="line">		e.setName(<span class="string">"周星驰"</span>);</span><br><span class="line">		e.setJob(<span class="string">"高级工程师"</span>);</span><br><span class="line">		e.show();</span><br><span class="line"></span><br><span class="line">		<span class="comment">//带参</span></span><br><span class="line">		Employee e2 = <span class="keyword">new</span> Employee(<span class="string">"itcast003"</span>,<span class="string">"刘德华"</span>,<span class="string">"挖掘机工程师"</span>);</span><br><span class="line">		e2.show();</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="java类中的成员变量和方法访问权限"><a href="#java类中的成员变量和方法访问权限" class="headerlink" title="java类中的成员变量和方法访问权限"></a>java类中的成员变量和方法访问权限</h3><table><thead><tr><th style="text-align:center">关键词</th><th style="text-align:center">同一个类</th><th style="text-align:center">同一个包</th><th style="text-align:center">不同包中的子类</th><th style="text-align:center">不同包中的非子类</th></tr></thead><tbody><tr><td style="text-align:center">private</td><td style="text-align:center">√</td><td style="text-align:center">&ensp;</td><td style="text-align:center">&ensp;</td><td style="text-align:center">&ensp;</td></tr><tr><td style="text-align:center">default</td><td style="text-align:center">√</td><td style="text-align:center">√</td><td style="text-align:center">&ensp;</td><td style="text-align:center">&ensp;</td></tr><tr><td style="text-align:center">protected</td><td style="text-align:center">√</td><td style="text-align:center">√</td><td style="text-align:center">√</td><td style="text-align:center">&ensp;</td></tr><tr><td style="text-align:center">public</td><td style="text-align:center">√</td><td style="text-align:center">√</td><td style="text-align:center">√</td><td style="text-align:center">√</td></tr></tbody></table></div><div class="popular-posts-header"><i class="fa fa-link"></i> 相关文章</div><!--[if !IE]><!--><details><summary style="cursor:pointer">点击查看</summary><!--<![endif]--><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\fanxing.html" rel="bookmark">java泛型test</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\SingleThreadDown.html" rel="bookmark">java实现一个单线程的资源下载器</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\byteio.html" rel="bookmark">文件加密解密（字节流）</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\JavaException.html" rel="bookmark">模拟借书系统（java异常练习）</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\HelloJava.html" rel="bookmark">用记事本编写第一个java程序</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\RegexDemo.html" rel="bookmark">java正则表达式练习</a></div></li></ul><!--[if IE]><!--></details><!--<![endif]--><div><div style="padding:10px 0;margin:20px auto;width:90%;text-align:center"><div>欢迎关注公众号，感谢支持 ！</div><button id="rewardButton" disable="enable" onclick="var qr = document.getElementById(&quot;QR&quot;); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}"><span>赞赏</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/images/wechatpay.gif" alt="李瑞豪 微信支付"><p>微信支付</p></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="/images/alipay.gif" alt="李瑞豪 支付宝"><p>支付宝</p></div></div></div></div><div class="copyright-box"><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者： </strong>李瑞豪</li><li><strong>修改时间： </strong>2019-10-19 10:54:56</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="https://lruihao.cn/posts/duixiang.html" title="面向对象基础知识总结">https://lruihao.cn/posts/duixiang.html</a></li><li class="post-copyright-license"><strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noopener noreferrer" target="_blank"><i class="fa fa-fw fa-creative-commons"></i> BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li></ul></div><div class="post-tags"><a href="/tags/JAVA/" rel="tag"><i class="fa fa-tag"></i> JAVA</a> <a href="/tags/面向对象/" rel="tag"><i class="fa fa-tag"></i> 面向对象</a></div><footer class="post-footer"><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/posts/xunhuan.html" rel="next" title="java水仙花数（循环）"><i class="fa fa-chevron-left"></i> java水仙花数（循环）</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/posts/mathclass.html" rel="prev" title="java猜数字小游戏（Math类）">java猜数字小游戏（Math类） <i class="fa fa-chevron-right"></i></a></div></div></footer></div></article></div></div><div class="comments" id="comments"></div></div><div class="sidebar-toggle"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div><aside id="sidebar" class="sidebar"><div id="sidebar-dimmer"></div><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">文章目录</li><li class="sidebar-nav-overview" data-target="site-overview-wrap">站点概览</li></ul><div class="site-overview-wrap sidebar-panel"><div class="site-overview"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" src="/images/avatar.png" alt="李瑞豪"><p class="site-author-name" itemprop="name">李瑞豪</p><p class="site-description motion-element" itemprop="description">从ACM到Web，分享程序、技巧、干货，记录心情、学习、成长！</p></div><nav class="site-state motion-element"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">176</span> <span class="site-state-item-name">日志</span></a></div><div class="site-state-item site-state-categories"><a href="/docs/categories/index.html"><span class="site-state-item-count">25</span> <span class="site-state-item-name">分类</span></a></div><div class="site-state-item site-state-tags"><a href="/docs/tags/index.html"><span class="site-state-item-count">122</span> <span class="site-state-item-name">标签</span></a></div></nav><div class="feed-link motion-element"><a href="/atom.xml" rel="alternate"><i class="fa fa-rss"></i> RSS</a></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/Lruihao" title="GitHub &rarr; https://github.com/Lruihao" rel="external nofollow noopener noreferrer" target="_blank"><i class="fa fa-fw fa-github"></i></a> </span><span class="links-of-author-item"><a href="https://blog.csdn.net/qq_39520417" title="CSDN &rarr; https://blog.csdn.net/qq_39520417" rel="external nofollow noopener noreferrer" target="_blank"><i class="fa fa-fw fa-contao"></i></a> </span><span class="links-of-author-item"><a href="https://weibo.com/liahao" title="微博 &rarr; https://weibo.com/liahao" rel="external nofollow noopener noreferrer" target="_blank"><i class="fa fa-fw fa-weibo"></i></a> </span><span class="links-of-author-item"><a href="/images/qq.jpg" title="QQ &rarr; /images/qq.jpg"><i class="fa fa-fw fa-qq"></i></a> </span><span class="links-of-author-item"><a href="mailto:1074627678@qq.com" title="E-Mail &rarr; mailto:1074627678@qq.com" rel="external nofollow noopener noreferrer" target="_blank"><i class="fa fa-fw fa-envelope"></i></a></span></div><div class="cc-license motion-element" itemprop="license"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="external nofollow noopener noreferrer" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a></div><div class="links-of-blogroll motion-element links-of-blogroll-inline"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-globe"></i> 书签</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="/docs/donators/" title="/docs/donators/">赞助记录</a>&ensp;</li><li class="links-of-blogroll-item"><a href="/docs/friends/" title="/docs/friends/">友情链接</a>&ensp;</li><li class="links-of-blogroll-item"><a href="/posts/links.html" title="/posts/links.html">收藏夹</a>&ensp;</li><li class="links-of-blogroll-item"><a href="/posts/font-mmt.html" title="/posts/font-mmt.html">MMT</a>&ensp;</li><li class="links-of-blogroll-item"><a href="http://md.lruihao.cn" title="http://md.lruihao.cn" rel="external nofollow noopener noreferrer" target="_blank">WXMD</a>&ensp;</li><li class="links-of-blogroll-item"><a href="/posts/webbiji.html" title="/posts/webbiji.html">WEB</a>&ensp;</li></ul></div></div></div><div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#面向对象思想-理解"><span class="nav-number">1.</span> <span class="nav-text">面向对象思想(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#类与对象-掌握"><span class="nav-number">2.</span> <span class="nav-text">类与对象(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#类的组成-掌握"><span class="nav-number">3.</span> <span class="nav-text">类的组成(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#类的使用-掌握"><span class="nav-number">4.</span> <span class="nav-text">类的使用(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#成员变量和局部变量的区别-理解"><span class="nav-number">5.</span> <span class="nav-text">成员变量和局部变量的区别(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#形式参数问题-理解"><span class="nav-number">6.</span> <span class="nav-text">形式参数问题(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#匿名对象-理解"><span class="nav-number">7.</span> <span class="nav-text">匿名对象(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#封装-掌握"><span class="nav-number">8.</span> <span class="nav-text">封装(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#private关键字-掌握"><span class="nav-number">9.</span> <span class="nav-text">private关键字(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#this关键字-掌握"><span class="nav-number">10.</span> <span class="nav-text">this关键字(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#构造方法-掌握"><span class="nav-number">11.</span> <span class="nav-text">构造方法(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#对象的初始化过程-理解"><span class="nav-number">12.</span> <span class="nav-text">对象的初始化过程(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#static关键字-掌握"><span class="nav-number">13.</span> <span class="nav-text">static关键字(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#静态成员变量和普通成员变量的区别-理解"><span class="nav-number">14.</span> <span class="nav-text">静态成员变量和普通成员变量的区别(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#main方法是静态的-理解"><span class="nav-number">15.</span> <span class="nav-text">main方法是静态的(理解)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实例"><span class="nav-number">16.</span> <span class="nav-text">实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#java类中的成员变量和方法访问权限"><span class="nav-number">17.</span> <span class="nav-text">java类中的成员变量和方法访问权限</span></a></li></ol></div></div></div></div></aside></div></main><footer id="footer" class="footer"><div class="footer-inner"><div class="copyright">Copyright &copy; 2018 – <span itemprop="copyrightYear">2020</span> <span class="with-love" id="animate"><i class="fa fa-heartbeat"></i> </span><span class="author" itemprop="copyrightHolder">LRH.&ensp;</span> <span title="博客总字数"><i class="fa fa-edit"></i>&ensp;<span class="post-count">115.7k</span>字</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span class="busuanzi-value" id="busuanzi_value_site_uv"><i class="fa fa-spinner fa-spin"></i></span>&thinsp;人次 </span><span class="post-meta-divider">|</span> <span class="run-times" title="网站运行时间">载入天数时分秒...</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span class="busuanzi-value" id="busuanzi_value_site_pv"><i class="fa fa-spinner fa-spin"></i></span>&thinsp;次</span></div><div class="weixin-box"><div class="weixin-menu"><div class="weixin-hover"><div class="weixin-description">微信扫一扫，订阅本博客</div></div></div></div><div class="beian" style="display:inline-block;height:20px;line-height:20px"><a target="_blank" href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=43030402000254" rel="external nofollow noopener noreferrer"><img src="/images/gov.png" style="float:left" alt="公安">湘公网安备43030402000254号</a> <span class="post-meta-divider" style="color:#555">|</span> <span><a href="http://www.beian.miit.gov.cn" target="_blank" rel="external nofollow noopener noreferrer">湘ICP备18020535号</a></span></div></div></footer><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span id="scrollpercent"><span>0</span>%</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js"></script><script type="text/javascript" src="/lib/reading_progress/reading_progress.js"></script><script type="text/javascript" src="/js/src/utils.js?v=6.7.0"></script><script type="text/javascript" src="/js/src/motion.js?v=6.7.0"></script><script type="text/javascript" src="/js/src/affix.js?v=6.7.0"></script><script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.7.0"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=6.7.0"></script><script type="text/javascript" src="/js/src/post-details.js?v=6.7.0"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=6.7.0"></script><script>$(".highlight").each(function(t,e){var n=$("<div>").addClass("highlight-wrap");$(e).after(n),n.append($("<button>").addClass("copy-btn").append("复制").on("click",function(t){var e=$(this).parent().find(".code").find(".line").map(function(t,e){return $(e).text()}).toArray().join("\n"),n=document.createElement("textarea");document.body.appendChild(n),n.style.position="absolute",n.style.top="0px",n.style.left="0px",n.value=e,n.select(),n.focus();var o=document.execCommand("copy");document.body.removeChild(n),o?$(this).text("复制成功"):$(this).text("复制失败"),$(this).blur()})).on("mouseleave",function(t){var e=$(this).find(".copy-btn");setTimeout(function(){e.text("复制")},300)}).append(e)})</script><script>$("body").find("pre.mermaid").length&&$.ajax({type:"GET",url:"//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js",dataType:"script",cache:!0,success:function(){mermaid.initialize({theme:"forest",logLevel:3,flowchart:{curve:"linear"},gantt:{axisFormat:"%m/%d/%Y"},sequence:{actorMargin:50}})}})</script><script>function createTime(){var e=new Date,t=new Date("05/28/2018 20:01:01"),n=(e-t)/1e3,i=Math.floor(n/60/60/24),r=Math.floor(n/60/60-24*i),h=Math.floor(n/60-1440*i-60*r),a=Math.floor((e-t)/1e3-86400*i-3600*r-60*h);1===String(r).length&&(r="0"+r),1===String(h).length&&(h="0"+h),1===String(a).length&&(a="0"+a),document.querySelector(".run-times").innerHTML=i+"&thinsp;天&thinsp;"+r+"&thinsp;时&thinsp;"+h+"&thinsp;分&thinsp;"+a+"&thinsp;秒"}if(document.hidden)clearInterval(siteTime);else var siteTime=setInterval("createTime()",500)</script><script type="text/javascript" src="/js/src/console.js"></script><script async type="text/javascript" src="/js/src/night.js"></script><div class="cover"></div><script async type="text/javascript" src="/js/src/crash-cheat.js"></script><script src="/js/src/activate-power-mode.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE)</script><script type="text/javascript" src="/js/src/love.js"></script><script type="text/javascript" src="/js/src/link-card.js" defer></script><script type="text/javascript" src="/js/src/pageQRcode.js" defer></script><script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script><script src="//unpkg.com/valine/dist/Valine.min.js"></script><script>var GUEST=["nick","mail","link"],guest="nick,mail,link";guest=guest.split(",").filter(function(e){return-1<GUEST.indexOf(e)}),new Valine({el:"#comments",verify:!1,notify:!1,appId:"7HwTRT0Q0Tfrat6ugrT6P67c-gzGzoHsz",appKey:"mhTY1kuUmviCtQwkwOASfsfD",placeholder:"ヾﾉ≧∀≦)o~ 有事请留言！\n评论功能以邮件作为通知方式！\n如有必要请填写正确邮箱！",avatar:"wavatar",meta:guest,pageSize:"10",visitor:!0,lang:"zh-cn"})</script><script type="text/javascript">// Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });</script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/lib/pangu/dist/pangu.min.js?v=3.3"></script><script type="text/javascript">pangu.spacingPage()</script><script src="/lib/bookmark/bookmark.min.js?v=1.0"></script><script type="text/javascript">bookmark.scrollToMark("manual","#更多")</script><script>!function(e){var r=Array.prototype.slice.call(document.querySelectorAll("img[data-original]"));function t(){for(var c=0;c<r.length;c++)t=r[c],void 0,0<=(n=t.getBoundingClientRect()).top&&0<=n.left&&n.top<=(e.innerHeight||document.documentElement.clientHeight)&&function(){var t,n,e,i,o=r[c];t=o,n=function(){r=r.filter(function(t){return o!==t})},e=new Image,i=t.getAttribute("data-original"),e.onload=function(){t.src=i,n&&n()},e.src=i}();var t,n}t(),e.addEventListener("scroll",function(){!function(t,n){clearTimeout(t.tId),t.tId=setTimeout(function(){t.call(n)},500)}(t,e)})}(this);</script></body></html>